package com.app.utiles.other;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import com.app.ui.activity.base.BaseApplication;

/**
 * Created by Administrator on 2017/8/2.
 */

public class APKInfo {
    private static APKInfo aPKInfo;
    //版本名称，版本号
    private String versionCode, versionName;
    //手机系统版本,app目标版本
    private int sdkVersion, targetSdkVersion;
    private Context context;
    public DisplayMetrics displayMetrics;

    public static APKInfo getInstance() {
        if (aPKInfo == null) {
            aPKInfo = new APKInfo();
        }
        return aPKInfo;
    }

    public APKInfo() {
        context = BaseApplication.context;
        displayMetrics = context.getResources().getDisplayMetrics();
        getVersion();
    }

    private void getVersion() {
        try {
            PackageManager manager = context.getPackageManager();
            String name = context.getPackageName();
            PackageInfo info = manager.getPackageInfo(name, 0);
            //版本名称
            versionName = info.versionName;
            //版本号
            versionCode = String.valueOf(info.versionCode);
        } catch (Exception e) {
            e.printStackTrace();

        }
        //手机系统版本
        sdkVersion = Build.VERSION.SDK_INT;
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
            //app目标版本
            targetSdkVersion = info.applicationInfo.targetSdkVersion;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    //===============================================================
    //dip转px
    public float getDIPTOPX(int dip) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, displayMetrics);
    }

    /**
     * 监听程序是否在后台（在Application 调用）
     *
     * @param application
     */
    public void registerCallbacks(Application application) {
        if (isRegister) {
            return;
        }
        isRegister = true;
        application.registerActivityLifecycleCallbacks(new ActivityLifecycleListener());
    }

    //true: 在后台运行
    public boolean isBackground() {
        boolean isBack = refCount == 0;
        String hint = isBack ? "后" : "前";
        DLog.e("APKInfo", "------" + hint + refCount);
        return isBack;
    }

    /**
     * 已测试：加载图片有效
     *
     * @param resourceId 资源id
     * @return 资源path(比如图片)
     */
    public String getResourcePath(int resourceId) {
        return "android.resource://" + context.getApplicationContext().getPackageName() + "/" + resourceId;
    }

    /**
     * @param resourceName 资源名称
     * @return 资源id
     */
    public int getResourceId(String resourceName) {
        ApplicationInfo applicantionInfo = context.getApplicationInfo();
        //取得该图片的id  (name 指定图片的名称，"drawable"指定图片存放的目录，appInfo.packageName指定应用程序的包名)
        int resID = context.getResources().getIdentifier(resourceName, "drawable", applicantionInfo.packageName);
        if (resID == 0) {
            resID = context.getResources().getIdentifier(resourceName, "mipmap", applicantionInfo.packageName);
        }
        return resID;
    }

    /**
     * @param name Assts下的资源名称（包括后缀名）
     * @return 资源Path
     */
    public static String getAsstsPath(String name) {
        String path = "file:///android_asset/" + name;
        return path;
    }

    //获取屏幕宽度
    public float getScreenWidthPixels() {
        return displayMetrics.widthPixels;
    }

    //获取屏幕高度
    public float getScreenHeightPixels() {
        return displayMetrics.heightPixels;
    }

    public String getVersionName() {
        return versionName;
    }

    public String getVersionCode() {
        return versionCode;
    }

    public int getSdkVersion() {
        return sdkVersion;
    }

    public int getTargetSdkVersion() {
        return targetSdkVersion;
    }

    //===========启动activity监听===========
    private int refCount = 0;
    private boolean isRegister;

    class ActivityLifecycleListener implements Application.ActivityLifecycleCallbacks {


        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(Activity activity) {
            refCount++;
        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {
            refCount--;

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    }

}
